/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.ouertech.android.agm.lib.ui.base.BaseUIFragment;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseSystemActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.base.BaseNiudanFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/28.
 * desc    :
 */
public class NDNiudanActivity extends BaseSystemActivity {
    //
    public static final int FRAGMENT_CENTER = 0;
    //
    public static final int FRAGMENT_OPTIONS = FRAGMENT_CENTER + 1;

    private int mFragment;
    private BaseNiudanFragment mStackTopFragment;
    //左门
    @Bind(R.id.niudan_id_door) View mViewDoor;
    //左门
    @Bind(R.id.niudan_id_left) View mViewLeft;
    //右门
    @Bind(R.id.niudan_id_right) View mViewRight;


    //动画平移距离
    private float mDistance;
    //门是否关闭
    private boolean mDoorClose;

    /**
     * 门打开监听器
     */
    public interface OnDoorOpenListener {
        void onOpen();
    }

    /**
     * 门关闭监听器
     */
    public interface OnDoorCloseListener {
        void onClose();
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mFragment = getIntent().getIntExtra(CstOuer.KEY.FRAGMENT, FRAGMENT_CENTER);
        mDoorClose = true;

        UtilDevice.Device device = UtilDevice.getDevice(this);
        mDistance = device.getWidth()/2f - 40 * device.getDensity();
    }

    @Override
    protected void initSystem() {
        //禁用设置状态栏背景颜色
        setSystemBarEnabled(false);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_base_niudan);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        if(mFragment == FRAGMENT_CENTER) {//显示扭蛋中心
            OuerDispatcher.presentCenterFragment(this);
        } else if(mFragment == FRAGMENT_OPTIONS) {//显示扭蛋列表
            OuerDispatcher.presentOptionsFragment(this);
        }
    }



    /**
     * 当前栈顶的fragment
     */
    public void setStackTopFragment(BaseNiudanFragment fragment) {
        mStackTopFragment = fragment;
    }

    /**
     * 返回
     */
    @Override
    public void onBackPressed() {
        if(mStackTopFragment != null){
            mStackTopFragment.onBackPressed();
        }
    }

    /**
     * 返回
     */
    @OnClick(R.id.niudan_id_navigation)
    public void onNavigation() {
        onBackPressed();
    }

    /**
     *
     * @param visible
     */
    public void setDoorVisible(boolean visible) {
        if(visible) {
            mViewDoor.setVisibility(View.VISIBLE);
        } else {
            mViewDoor.setVisibility(View.GONE);
        }
    }

    @Override
    public void finish() {
        if(getSupportFragmentManager().getBackStackEntryCount() == 0){
            super.finish();
        }else{
            getSupportFragmentManager().popBackStack();
        }
    }

    /**
     * 打开门
     */
    public void openDoor(final OnDoorOpenListener listener, long delay) {
        if(!mDoorClose) {
            return;
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();

                //关闭左侧门
                TranslateAnimation animLeft = new TranslateAnimation(0, -mDistance, 0, 0);
                animLeft.setFillAfter(true);
                animLeft.setDuration(700);
                animLeft.setInterpolator(interpolator);
                animLeft.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mDoorClose = false;
                        if(listener != null) {
                            listener.onOpen();
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mViewLeft.startAnimation(animLeft);
                //打开右侧门
                TranslateAnimation animRight = new TranslateAnimation(0, mDistance, 0, 0);
                animRight.setFillAfter(true);
                animRight.setDuration(700);
                animRight.setInterpolator(interpolator);
                mViewRight.startAnimation(animRight);
            }
        }, delay);
    }

    /**
     * 关闭门
     */
    public void closeDoor(final OnDoorCloseListener listener, long delay) {
        if(mDoorClose) {
            return;
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
                //关闭左侧门
                TranslateAnimation animLeft = new TranslateAnimation(-mDistance, 0, 0, 0);
                animLeft.setFillAfter(true);
                animLeft.setDuration(700);
                animLeft.setInterpolator(interpolator);
                animLeft.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mDoorClose = true;
                        if(listener != null) {
                            listener.onClose();
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mViewLeft.startAnimation(animLeft);
                //关闭右侧门
                TranslateAnimation animRight = new TranslateAnimation(mDistance, 0, 0, 0);
                animRight.setFillAfter(true);
                animRight.setDuration(700);
                animRight.setInterpolator(interpolator);
                mViewRight.startAnimation(animRight);
            }
        }, delay);

    }


    /**
     * 添加片段
     * @param fragmentClzz
     */
    public void addFragment(Class<? extends BaseUIFragment> fragmentClzz) {
        addFragment(fragmentClzz, null);
    }

    /**
     * 添加片段
     * @param fragmentClzz
     * @param bundle
     */
    public void addFragment(Class<? extends BaseUIFragment> fragmentClzz, Bundle bundle) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        if(fm.getFragments() != null) {
            ft.addToBackStack(null);
        }

        try {
            BaseUIFragment fragment = (BaseUIFragment)fragmentClzz.newInstance();
            if(bundle != null) {
                fragment.setArguments(bundle);
            }

            ft.replace(R.id.niudan_id_back, fragment).commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }



}
